package com.example.service;

import com.example.bean.Goods;
import com.example.bean.ResultVo;
import com.example.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class GoodsService {

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 删除给定ID的商品
     *
     * @param gId
     */
    public void deleteById(int gId) {
        goodsMapper.deleteById(gId);
    }

    /**
     * 新增商品
     *
     * @param goods
     */
    public void insertGoods(Goods goods) {
        goodsMapper.insertGoods(goods);
    }

    /**
     * 修改商品
     * @param goods
     */
    public void updateImgGoods(Goods goods){
        goodsMapper.updateImgGoods(goods);
    }

    /**
     * 查询商家所属的所有商品
     *
     * @return
     */
    public ResultVo selectAllGoods(int mId,String likeName) {
        return new ResultVo(goodsMapper.selectAll(mId,likeName));
    }

    /**
     * 用户查询商家所属的所有商品
     *
     * @return
     */
    public ResultVo UserSelectAllGoods(int mId) {
        return new ResultVo(goodsMapper.UserSelectAll(mId));
    }

    /**
     * 用户端分页查询所有商品
     * @param gName 前台搜索框中的名字
     * @param currentPage 当前页
     * @param currentCount  一页显示的条数
     * @return
     */
    public ResultVo selectTotalGoods(String gName, int currentPage, int currentCount) {
        return new ResultVo(goodsMapper.selectTotal(gName,currentCount * (currentPage - 1),currentCount));
    }

    /**
     * 后台搜索框查询商品
     *
     * @param likeName
     * @return
     */
    public ResultVo selectLikeName(Integer mId,String likeName) {
        return new ResultVo(goodsMapper.selectLikeName(mId,likeName));
    }


    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResultVo findpage(Integer mId, Integer pageNum, Integer pageSize,String likeName) {
        pageNum = (pageNum - 1) * 5;
        return new ResultVo(goodsMapper.findPage(mId, pageNum, pageSize,likeName));
    }

    /**
     * 通过gId查询商品
     *
     * @param gId
     * @return
     */
    public ResultVo selectBygId(int gId) {
        return new ResultVo(goodsMapper.selectGoodsBygId(gId));
    }

    /**
     * 商品状态更换
     * @param gId
     * @param gState
     */
    public void updateGoodsState(int gId,String gState){
        goodsMapper.updateGoodsState(gId,gState);
    }

    /**
     * 用户端查询商品图片
     * @return 返回图片地址集合
     */
    public List<String> selectGoodImgs() {
        return  goodsMapper.selectGoodImgs();
    }

    /**
     * 用户查询全部在架商品的数量
     * @param gName 商品名
     * @return
     */
    public int selectTotalGoodsCount(String gName) {
        return goodsMapper.selectTotalGoodsCount(gName);
    }
}
